#pragma once
#include <iostream>
#include <pthread.h>

namespace MutexModule {
    class Mutex {
    public:
        Mutex() {
            pthread_mutex_init(&_mutex, nullptr);
        }

        // 加锁
        void Lock() {
            int n = pthread_mutex_lock(&_mutex);
            (void)n;
        }

        // 解锁
        void Unlock() {
            int n = pthread_mutex_unlock(&_mutex);
            (void)n;
        }

        pthread_mutex_t* Get() const {
            return const_cast<pthread_mutex_t*>(&_mutex);
        }

        ~Mutex() {
            pthread_mutex_destroy(&_mutex);
        }
    private:
        pthread_mutex_t _mutex; // 使用 pthread 互斥锁
    };

    // RAII 风格
    class LockGuard {
    public:
        LockGuard(Mutex& mutex) : _mutex(mutex) {
            _mutex.Lock();
        }
        ~LockGuard() {
            _mutex.Unlock();
        }
    private:
        Mutex& _mutex; // 引用 -- 外部传入 -- 保证"单一"
    };
}